package connect;

import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.io.FileInputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import common.Utility;

public class ConnectionHelper
{		
	public ConnectionHelper()
	{
		
	}
		
	public static Connection getConnection() throws Exception {
		Connection connection = null;
		String driverName = "";
		String serverName = "";
		String portNumber = "";
		String dbname = "";
		String username = "";
		String password = "";
		String url = "";
		try 
		{
			
			Properties prop = new Properties();		    
		    prop.load(new FileInputStream("config.properties"));
		    String typeConn = prop.getProperty("typeconn");
		    if(typeConn.equals(Utility._ORACLE))
		    {
		    	driverName = "oracle.jdbc.driver.OracleDriver";
			    Class.forName(driverName);
			    serverName = prop.getProperty("database");		    
			    portNumber = prop.getProperty("port");
			    dbname = prop.getProperty("dbname");
			    username = prop.getProperty("dbuser");		    
			    password = decrypt(prop.getProperty("dbpassword"), "ffpcFFpc12345678") ;		    
			    url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + "/" + dbname;
		    }			
		    else if (typeConn.equals(Utility._MYSQL))
		    {
		    	driverName ="com.mysql.jdbc.Driver";
		    	Class.forName(driverName).newInstance();
		    	serverName = prop.getProperty("database");
		    	portNumber = prop.getProperty("port");
		    	dbname = prop.getProperty("dbname");		    	
		    	username = prop.getProperty("dbuser");		    
			    password = decrypt(prop.getProperty("dbpassword"), "ffpcFFpc12345678") ;
			    url = "jdbc:mysql://" + serverName + ":" + portNumber + "/" +dbname;			    
		    }
		    else if (typeConn.equals(Utility._SQL))
		    {		    	
		    	driverName ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
		    	Class.forName(driverName);
		    	serverName = prop.getProperty("database");
		    	portNumber = prop.getProperty("port");
		    	dbname = prop.getProperty("dbname");		    	
		    	username = prop.getProperty("dbuser");		    
			    password = decrypt(prop.getProperty("dbpassword"), "ffpcFFpc12345678") ;
			    url = "jdbc:sqlserver://" + serverName + ":" + portNumber + ";databaseName=" + dbname + ";selectMethod=cursor";
		    }
		    
		    if(connection == null)
		    	connection = DriverManager.getConnection(url, username, password);
		    
		} 
		catch (IOException ex) 
		{
    		ex.printStackTrace();
    		throw new SQLException(ex.getMessage());
        }
		catch (ClassNotFoundException e) 
        {		    
        	e.printStackTrace();
        	throw new SQLException(e.getMessage());
		} catch (SQLException e) 
		{		    
			e.printStackTrace();
			throw new SQLException(e.getMessage());
		}				
		return connection;
	}		
	
	
	public static String encrypt(String PassWord, String keyInput)
	{
		 String output = "" ;
		 try 
		 {
			 SecretKeySpec key = new SecretKeySpec(keyInput.getBytes(), "AES"); 
			 AlgorithmParameterSpec paramSpec = new  IvParameterSpec(keyInput.getBytes()); 
			 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 			 
			 cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
			 
			 byte[] ecrypted = cipher.doFinal(PassWord.getBytes());
			 
			 output =  new BASE64Encoder().encode(ecrypted); 
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
		return output ;
	}
	
	public static String decrypt(String PassWord, String keyInput)
	{
		 String reOut ="" ;
		 try 
		 {
			 SecretKeySpec key = new SecretKeySpec(keyInput.getBytes(), "AES"); 
			 AlgorithmParameterSpec paramSpec = new  IvParameterSpec(keyInput.getBytes()); 
			 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 			 
			 cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
			 
			 byte[] output =  new BASE64Decoder().decodeBuffer(PassWord);			 
			 byte[] decrypted = cipher.doFinal(output); 
			 reOut = new String(decrypted) ;
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
		return reOut ;
	}
	
	public static void close(Connection connection)
	{
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}